const graph = require('./graph');


const bfs = (graph, n) => {
	const set = new Set()
	const queue = [n]
	set.add(n) // 入队后就将节点放进访问的集合中
	while (queue.length) {
		const elem = queue.shift()
		console.log(elem)
		graph[elem].forEach((child) => {
			if(!set.has(child)) {
				queue.push(child)
				// 入队后就算访问过了, 需要加到set中
				set.add(child)
			}
		})
	}
}
console.log(bfs(graph, 2))
// const visited = new Set();
// const q = [2];
// while (q.length) {
//     const n = q.shift();
//     console.log(n);
//     visited.add(n);
//     graph[n].forEach(c => {
//         if (!visited.has(c)) {
//             q.push(c);
//         }
//     });
// }
